package com.example.bmipractice;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.math.BigDecimal;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EditText w = findViewById(R.id.weight);
        EditText h = findViewById(R.id.height);
        TextView bmi = findViewById(R.id.bmi);
        Button btn = findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener(){
            @Override
            public  void onClick(View view) {
                double weight = Double.parseDouble(w.getText().toString());
                double height = Double.parseDouble(h.getText().toString());
                double firstBMI = weight / (height * height);
                BigDecimal a = new BigDecimal(firstBMI);
                double finalBMI = a.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                if (finalBMI < 18.50) {
                    bmi.setText("BMI指数为:" + finalBMI +" 您的BMI指数偏低，建议您适当增加饮食来提高体重。");
                }
                else if(finalBMI>24.90){
                    bmi.setText("BMI指数为:" + finalBMI +" 您的BMI指数偏高，建议您适当加强锻炼来减少体重。");
                }
                else{
                    bmi.setText("BMI指数为:" + finalBMI +" 这里做了点小修改");
                }
            }
        });
    }
}